package com.xyq.common.core.domain;

import com.xyq.common.core.enums.ResultCode;
import lombok.Data;

@Data
public class Result<T> {
    private Integer code;  // 状态码
    private String message; // 消息
    private T data; // 数据

    // 无参的 成功方法
    public static <T> Result<T> success() {
        return assembleResult(null, ResultCode.SUCCESS);
    }

    // 有 data数据的成功方法
    public static <T> Result<T> success(T data) {
        return assembleResult(data, ResultCode.SUCCESS);
    }

    // 无参的失败方法
    public static <T> Result<T> fail() {
        return assembleResult(null, ResultCode.FAILED);
    }

    // 有参数的 失败方法
    public static <T> Result<T> fail(ResultCode resultCode) {
        return assembleResult(null, resultCode);
    }

    private static <T> Result<T> assembleResult(T data, ResultCode resultCode) {
        Result<T> result = new Result<>();
        // 根据参数 , 构造响应
        result.setData(data);
        result.setCode(resultCode.getCode());
        result.setMessage(resultCode.getMessage());
        return result;
    }

    public static <T> Result<T> fail(int code, String msg) {
        return assembleResult(code, msg, null);
    }

    private static <T> Result<T> assembleResult(int code, String msg, T data) {
        Result<T> r = new Result<>();
        r.setCode(code);
        r.setData(data);
        r.setMessage(msg);
        return r;
    }

    }
